using Entity;
using Utils;

namespace Mapper
{
    class UserMapper
    {

        private const string TABLE_NAME = "user";
        public UserMapper()
        {
            MySQLUtil.CreateConnection();
        }

        public User GetUserById(long userId)
        {
            return MySQLUtil.SelectData<User>(TABLE_NAME, "*", "userId = " + userId)[0];
        }

        public long AddUser(User user)
        {
            return MySQLUtil.AddData<User>(user, TABLE_NAME);
        }

        public void ChangeCoinNumByUserId(long userId, long coinNum)
        {
            string sql = $"update user set coin = {coinNum} where userId = {userId}";
            MySQLUtil.UpdateData(sql);
        }
        
        public void ChangeDustNumByUserId(long userId, long dustNum)
        {
            string sql = $"update user set dust = {dustNum} where userId = {userId}";
            MySQLUtil.UpdateData(sql);
        }

        public void UpdateData(User user)
        {
            MySQLUtil.UpdateData<User>(TABLE_NAME, "userId" , user.UserId, user);
        }

        public void CloseConnection()
        {
            MySQLUtil.Close();
        }
    }
}

